<?php
require_once ('../utility/appConstsPoc.php');
/**
 * GCM class that manages interaction with Server
 *
 * @author relsellglobal
 *        
 */
class GCMEAS {
	var $registrationId;
	
	function GCMEAS() {
		
	}

	function registerDeviceAtServer($registrationId, $loggedinuserid) {
		$appConstants = appConstsPOCClass :: getInstance();
		$appConstants->initialize ();
		$db_handle = mysql_connect ( $appConstants->SERVER, $appConstants->DBUSERNAME, $appConstants->DBPASSWORD );
		$db_found = mysql_select_db ( $appConstants->DBNAME, $db_handle );
		$resultRows = 0;
		if ($db_found) {
			$SQL = "INSERT INTO registrationids_table (userid ,registrationid) VALUES ('$loggedinuserid','$registrationId') ";
			if ($appConstants->DEBUG) {
				echo "success"."<br/>";
				echo $SQL . "<br/>";
			}
			mysql_query ( $SQL );
			$resultRows = mysql_affected_rows ();
		}
		$arr = array ();
		$arr ['deviceregistered'] = 0;
		if ($resultRows > 0) {
			$arr ['deviceregistered'] = 1;
		}
		return $arr;
	}
	function getDataFromDevice($regsitrationId, $lat, $long, $time) {
	}
	function sendMessageToDevice($messageType) {
		$registrationIdArray = $this ->getRegIds($messageType);
		//$messageArray = $this->getDataForSendingViaGCM($messageType);
		$messageArray = array("Hello Rachit");
		$formattedMessageArray = array();
		$regIdArray = array();
		$messageArrStr = json_encode($messageArray);
		$strRegIds = "";
		$count  = count($registrationIdArray);
		for ($i=0; $i<$count;$i++) {
			$regIdArray[$i] = $registrationIdArray[$i]['registrationid']; 
		}
		foreach($messageArray as $messageArrElement) {
			array_push($formattedMessageArray,$messageArrElement);
		}
		$encodeArr = json_encode($formattedMessageArray);
		$data = array (
				"registration_ids" => $regIdArray,
				"data" =>array("res_data"=> $encodeArr )
		);
		echo $data_string = json_encode ( $data );
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_HTTPHEADER, Array (
				"Content-Type: application/json",
				"Authorization:key=AIzaSyBgw3Fv69DFEmH-79xbry0uHnucJIAlUqQ" 
		) );
		curl_setopt ( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
		curl_setopt ( $ch, CURLOPT_POST, 1 );
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data_string );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
		
		echo $server_output = curl_exec ( $ch );
		
		curl_close ( $ch );
	}
	function emtpyTableRegistrationId($messageType) {
		$appConstants = appConstsPOCClass :: getInstance();
		$appConstants->initialize ();
		$localArr = array();
		$arr = array ();
		$db_handle = mysql_connect ( $appConstants->SERVER, $appConstants->DBUSERNAME, $appConstants->DBPASSWORD );
		$db_found = mysql_select_db ( $appConstants->DBNAME, $db_handle );
		$SQL = '';
		$localregid = '';
		echo mysql_error ();
		$i=0;
		if ($db_found) {
			$SQL = "TRUNCATE TABLE  `registrationids_table`";
			$result = mysql_query ( $SQL, $db_handle );
			mysql_close ( $db_handle );
		}
		return $result;
	}
	function getRegIds($messageType) {
		$appConstants = appConstsPOCClass :: getInstance();
		$appConstants->initialize ();
		$localArr = array();
		$arr = array ();
		$db_handle = mysql_connect ( $appConstants->SERVER, $appConstants->DBUSERNAME, $appConstants->DBPASSWORD );
		$db_found = mysql_select_db ( $appConstants->DBNAME, $db_handle );
		$SQL = '';
		$localregid = '';
		echo mysql_error ();
		$i=0;
		if ($db_found) {
			if ($messageType == 3) {
				$SQL = "SELECT distinct registrationid FROM  `registrationids_table` WHERE userid=3";
			}
			$result = mysql_query ( $SQL, $db_handle );
			$total = mysql_num_rows ( $result );
			if ($total > 0) {
				while ( $db_field = mysql_fetch_assoc ( $result ) ) {
					$localregid = $db_field ['registrationid'];
					$localArr ['registrationid'] = $localregid;
					$arr[$i] = $localArr;
					$i++;
				}
				mysql_close ( $db_handle );
			}
		}
		return $arr;
	}
	
	function getDataForSendingViaGCM ($messageType) {
		$appConstants = new appConstsClass ();
		$appConstants->initialize ();
		$localArr = array();
		$arr = array ();
		$db_handle = mysql_connect ( $appConstants->SERVER, $appConstants->DBUSERNAME, $appConstants->DBPASSWORD );
		$db_found = mysql_select_db ( $appConstants->DBNAME, $db_handle );
		$SQL = '';
		$localregid = '';
		echo mysql_error ();
		date_default_timezone_set('Asia/Calcutta');
		$presentTime = date('Y-m-d H:i:s');
		$i=0;
		if ($db_found) {
			if ($messageType == $appConstants->GCM_MESSAGE_FOR_PRODUCT_DATA_SUBMISSION) {
				$SQL = "SELECT * FROM product_table WHERE DATE( selling_date ) = DATE('$presentTime') ";
			}
			$result = mysql_query ( $SQL, $db_handle );
			$total = mysql_num_rows ( $result );
			if ($total > 0) {
				while ( $db_field = mysql_fetch_assoc ( $result ) ) {
					$localArr ['userid'] = $db_field ['userid'];
					$localArr ['product_type'] = $db_field['product_type'];
					$localArr ['product_quantity'] = $db_field['product_quantity'];
					$localArr ['selling_date'] = $db_field['selling_date'];
					$localArr ['latitude'] = $db_field['latitude'];
					$localArr ['longitude'] = $db_field['longitude'];
					$localArr ['target_achieved'] = $db_field['target_achieved'];
					$arr[$i] = $localArr;
					$i++;
				}
				mysql_close ( $db_handle );
			}
		}
		return $arr;
	}
}

?>